﻿<%@ Page Language="C#" MasterPageFile="~/NestedMasterPage.master" %>

<%@ Register TagPrefix="Page" TagName="Nav" Src="~/UserControls/PageControl.ascx" %>

<%@ Import Namespace="System.Web.Script.Serialization" %>

<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      ltlKeyword.Text = CommonUtil.GetRequestValue("Keyword");
      //初始化設定
      ControlnInit();
      //取得商品資料
      GenProductList();
    }
  }

  /// <summary>
  /// 初始化設定
  /// <summary>
  private void ControlnInit()
  {

  }

  /// <summary>
  /// 取得商品資料
  /// <summary>
  private void GenProductList()
  {    
    //每頁顯示商品數
    int pageSize = 15;
    //計算分頁的筆數區間
    int fromNo = 1 + pageSize * (this.Nav.currentPageNumber - 1);
    int fromTo = fromNo + pageSize - 1;

    B2C_MALL_HOME ws = new B2C_MALL_HOME();
    ws.Url = ConfigurationManager.AppSettings["B2C_MALL_HOME"].ToString(); 
    string jsonString =
      ws.B2C_MALL_searchProducts(ConfigurationManager.AppSettings["WebDeviceid"].ToString()
      , HttpContext.Current.User.Identity.Name, ltlKeyword.Text, fromNo.ToString(), fromTo.ToString());
    JObject jObject = JsonConvert.DeserializeObject<JObject>(jsonString);
    if (jObject["Is_Valid"].ToString() == "Y")
    {
      rep.DataSource = jObject["Prd_Ary"];
      rep.DataBind();
      this.Nav.TotalCount = jObject["Total_Count"].ToString();
      this.Nav.PagesCount = Math.Ceiling((Convert.ToDecimal(jObject["Total_Count"]) / pageSize)).ToString();      
    }
    else
    {
      rep.DataSource = null;
      rep.DataBind();
      this.Nav.TotalCount = "0";
      this.Nav.PagesCount = "0";
      lblKeyword.Text = jObject["Msg"].ToString();
    }
    //不滿5頁的去除其頁數
    this.Nav.DisplayPageNumber();
  }


  /// <summary>
  /// 商品資料設定
  /// <summary>
  protected void rep_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
  {

  }
  /// <summary>
  /// 每頁顯示商品數變更
  /// <summary>
  protected void lbtnPageSize_Click(object sender, EventArgs e)
  {
    //從第一頁開始
    this.Nav.currentPageNumber = 1;

    //將postback的檢視筆數設定顏色        
    LinkButton selectButton = ((LinkButton)sender);
    selectButton.CssClass = "current";
    //產生商品清單
    GenProductList();

  }

  /// <summary>
  /// 商品列表分頁變更
  /// <summary>
  protected void Nav_OnPageSelected(object sender)
  {
    GenProductList();
  }
    
    

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="NestedContent" runat="Server">
  <div class="breadcrumbs">
    <a href="../Home.aspx">首頁</a> / <a href="#" class="current">商品搜尋結果  </a>
  </div>
  <div class="filter-area">
    <div class="search-result-filter">
      <table>
        <tr>
          <th>關鍵字</th>
          <td>
            <asp:Literal ID="ltlKeyword" runat="server"></asp:Literal>
            <asp:Label ID="lblKeyword" runat="server" EnableViewState="false"></asp:Label></td>
        </tr>
      </table>
    </div>
  </div>
  <div class="page-tools">
    <!-- Page Nav User Control -->
    <div class="nums">
      <Page:Nav ID="Nav" runat="server" OnOnPageSelected="Nav_OnPageSelected" />
    </div>
  </div>
  <div class="search-result-container">
    <div class="list-table wish">
      <table>
        <tbody>
          <tr>
            <th class="spacer"></th>
            <th class="size1">商品圖</th>
            <th class="size2">商品名稱</th>
            <th>金額</th>
            <th class="spacer"></th>
          </tr>
          <asp:Repeater ID="rep" runat="server" OnItemDataBound="rep_ItemDataBound">
            <ItemTemplate>
              <%--<tr class="no-border">--%>
              <tr>
                <td class="spacer"></td>
                <td>
                  <a href='<%# "ProductDetail.aspx?B2C_CODE="+ Server.HtmlEncode(Eval("B2C_CODE").ToString()) %>' target="_blank">
                    <img src='<%# Server.HtmlEncode(Eval("IMAGE_PATH_M").ToString()) %>' alt='<%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %>' style="width: 50px" />
                  </a>
                </td>
                <td>
                  <p><a href='<%# "ProductDetail.aspx?B2C_CODE="+ Server.HtmlEncode(Eval("B2C_CODE").ToString()) %>' target="_blank">
                    <%# Server.HtmlEncode(Eval("B2C_CODE_NAME").ToString()) %></a></p>
                </td>
                <td>$ <%# Server.HtmlEncode(Eval("ACT_PRICE","{0:n0}").ToString()) %></td>
                <td class="spacer"></td>
              </tr>
            </ItemTemplate>
          </asp:Repeater>
        </tbody>
      </table>
    </div>
  </div>  
</asp:Content>

